/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.azon.ip.utils;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

/**
 *
 * @author Ahmad Fauzan
 */
public class ImageWriter {
    
    // WriteRGBImage
    public static BufferedImage WriteImageRGB(int[][] matrix) {
        BufferedImage new_img = new BufferedImage(matrix[0].length, matrix.length, BufferedImage.TYPE_INT_RGB);
        
        int[] data = new int[new_img.getWidth()*new_img.getHeight()];
        
        for(int x=0; x < new_img.getWidth(); x++) {
            for(int y=0; y < new_img.getHeight(); y++) {
                data[x*y] = matrix[y][x];
                new_img.setRGB(x, y, data[x*y]);
            }
        }
        
        return new_img;
    }
    
    // WriteGrayScaleImage
    public static BufferedImage writeImageGrayScale(int[][] matrix) {
        BufferedImage new_img = new BufferedImage(matrix[0].length, matrix.length, BufferedImage.TYPE_INT_RGB);
        
        int[] data = new int[new_img.getWidth()*new_img.getHeight()];
        
        for(int x=0; x < new_img.getWidth(); x++) {
            for(int y=0; y < new_img.getHeight(); y++) {
                data[x*y] = matrix[y][x];
                new_img.setRGB(x, y, data[x*y] + (data[x*y] << 8) + (data[x*y] << 16));
            }
        }
        return new_img;
    }
    
    // WriteImage
    public static void WriteImage(BufferedImage img, String img_path) throws IOException {
        img.flush();
        ImageIO.write(img, "jpg", new File(img_path));
    } 
}
